function [max_dir_dF, max_dir_ratio_change, max_ori_dF, max_ori_ratio_change] = write_dF_Images (dir_dF_sm, ori_dF_sm, dir_ratio, ori_ratio, varargin);

% write dF images & ratio change images as tif files
% auto scaled between zero and max value
% saving is optional when (..., 'outputpath', 'value') is specified

outputpath = [];

if ~isempty(varargin)
    numIndex = find(cellfun('isclass', varargin(1:end-1), 'char'));
    for ind = 1:length(numIndex)
        switch lower(varargin{numIndex(ind)})
            case 'outputpath'
                outputpath = varargin{numIndex(ind) + 1};
        end
    end
end

max_dir_dF = max(max(max(dir_dF_sm)));
max_dir_ratio_change = max(max(max(dir_ratio)));
max_ori_dF = max(max(max(ori_dF_sm)));
max_ori_ratio_change = max(max(max(ori_ratio)));

dim = size(dir_dF_sm);
ndir = dim(3);
nori = ndir / 2;

for i = 1:ndir
    if ~isempty(outputpath) 
        imwrite(dir_dF_sm(:,:,i) ./ max_dir_dF, ...
            fullfile(outputpath,[ 'dir_dF_', num2str(floor(i)), '.tif']));
    end
    
    map = dir_ratio(:,:,i) ./ max_dir_ratio_change;
    map(map > 1) = 1;
    if ~isempty(outputpath)
        imwrite(map, ...
            fullfile(outputpath,[ 'dir_ratio_', num2str(floor(i)), '.tif']));
    end
    clear map;
end

for i = 1:nori
    if ~isempty(outputpath) 
        imwrite(ori_dF_sm(:,:,i) ./ max_ori_dF, ...
            fullfile(outputpath,[ 'ori_dF_', num2str(floor(i)), '.tif']));
    end
    
    map = ori_ratio(:,:,i) ./ max_ori_ratio_change;
    map(map > 1) = 1;
    if ~isempty(outputpath) 
        imwrite(map, ...
            fullfile(outputpath,[ 'ori_ratio_', num2str(floor(i)), '.tif']));
    end
    clear map;
end
